//
//  YQLStatusCell.swift
//  YQL_WeiBo
//
//  Created by apple on 2016/11/17.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SDWebImage
//图片视图的边距
let margin: CGFloat = 8
//图片视图的最大宽
let maxWidth = ScreenWidth - margin * 2
//缩略的间距
let itemMargin: CGFloat = 3
//单个缩略图的宽高
let itemWidth = (maxWidth - CGFloat(itemMargin * 2)) / 3

class YQLStatusCell: UITableViewCell {

    //头像
    @IBOutlet weak var imageIcon: UIImageView!
    //用户名
    @IBOutlet weak var labelName: UILabel!
    //等级
    @IBOutlet weak var imageMemberShip: UIImageView!
    //发表微博时间
    @IBOutlet weak var labelTime: UILabel!
    //发表微博的来源
    @IBOutlet weak var labelSoure: UILabel!
    //微博内容
    @IBOutlet weak var labelContent: UILabel!
    //Vip等级
    @IBOutlet weak var imageVip: UIImageView!
    
    //缩略图
    @IBOutlet weak var pictureView: YQLPictureCollectionView!
    //缩略图的约束对象FlowLayOut
    @IBOutlet weak var pictureViewFlowLayout: UICollectionViewFlowLayout!
    
    //宽度的约束
    @IBOutlet weak var pictureWidthCons: NSLayoutConstraint!
    //高度的约束
    @IBOutlet weak var pictureHeightCons: NSLayoutConstraint!

    //通过重写顺传属性的didSet方法给控件赋值
    var cellViewModel: YQLStatusViewModel?{

        didSet{
            imageIcon.sd_setImage(with: cellViewModel?.iconUrl)
            labelName.text = cellViewModel?.status?.user?.name
            imageMemberShip.image = cellViewModel?.memberImage
            labelTime.text = cellViewModel?.status?.created_at
            labelSoure.text = cellViewModel?.status?.source
            labelContent.text = cellViewModel?.status?.text
            imageVip.image = cellViewModel?.verifiedImag
            //给缩略图传递数据
            pictureView.pictureArr = cellViewModel?.status?.pic_urls
            //修改缩略图的宽高约束
            let count = cellViewModel?.status?.pic_urls?.count ?? 0
            let size: CGSize = self.getSize(count: count)
            pictureWidthCons.constant = size.width
            pictureHeightCons.constant = size.height
        }
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        //对Cell进行设置
        self.backgroundColor = commonColor
        labelContent.preferredMaxLayoutWidth = ScreenWidth - margin * 2
        labelContent.numberOfLines = 0
        
        //对缩略图内item进行布局约束
        pictureViewFlowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        pictureViewFlowLayout.minimumLineSpacing = itemMargin
        pictureViewFlowLayout.minimumInteritemSpacing = itemMargin
    }
    
    func getSize(count: Int) -> CGSize {
        
        if count == 0 {
            return CGSize.zero
        }
        
        if count == 4 {
            let width = itemWidth * 2 + itemMargin
            return CGSize(width: width, height: width)
        }
        
        let row = CGFloat((count - 1) / 3) + 1
        let h = row * itemWidth + (row - 1) * itemMargin
        return CGSize(width: maxWidth, height: h)
    }
    
    

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
